<template>
  <div class="app-container">
    <!-- 进度计划管理弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <div class="left" v-for="(item,index) in form_col" :key="index">
          <el-form-item :label="item.title" :prop="item.prop">
            <el-input v-if="item.type === 'input'" v-model="form[item.prop]" :placeholder="item.placeholder" size="mini" :disabled="item.disabled" />
            <el-select v-if="item.type === 'select'" v-model="form[item.prop]" :placeholder="item.placeholder" clearable size="mini" style="width:100%">
              <el-option v-for="dict in item.options" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
            <el-date-picker v-if="item.type === 'date'" v-model="form[item.prop]" type="date" size="mini" :placeholder="item.placeholder" value-format="yyyy-MM-dd" style="width:100%">
            </el-date-picker>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getProgress, addProgress, updateProgress } from "@/api/business/project/progress";
export default {
  dicts: ['pm_stage_type'],
  data() {
    return {
      title: '新增',
      open: false,
      form: {
        projectId: null,
        statgeName: null,
        planStartDate: null,
        planEndDate: null,
        realEndDate: null,
        proStatus: null,
        memo: null,
      },
      form_col: [
      // { title: '项目id', prop: 'projectId', type: 'input', placeholder: '请输入项目id', options: [], show: true, disabled: false },
      { title: '节点名称', prop: 'statgeName', type: 'input', placeholder: '请输入节点名称', options: [], show: true, disabled: false },
      { title: '计划开始日期', prop: 'planStartDate', type: 'date', placeholder: '选择计划开始日期', options: [], show: true, disabled: false },
      { title: '计划完成日期', prop: 'planEndDate', type: 'date', placeholder: '选择计划完成日期', options: [], show: true, disabled: false },
      { title: '实际完成日期', prop: 'realEndDate', type: 'date', placeholder: '选择实际完成日期', options: [], show: true, disabled: false },
      { title: '当前进度', prop: 'proStatus', type: 'select', placeholder: '请选择进度', options: [], show: true, disabled: false },
      { title: '备注', prop: 'memo', type: 'input', placeholder: '请输入备注', options: [], show: true, disabled: false }],
      rules: {}
    }
  },
  mounted() {
    this.getoptions()
  },
  methods: {
    /**获取对应字典值 */
    getoptions() {
      this.form_col.forEach(element => {
        element.prop === 'proStatus' ? element.options = this.dict.type.pm_stage_type : []
      });
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        statgeName: null,
        planStartDate: null,
        planEndDate: null,
        realEndDate: null,
        proStatus: null,
        memo: null,
      };
      this.resetForm("form");
    },
    /**组件初始化方法 */
    init(row) {
      this.open = true;
      this.reset()
      if (row) {
        this.title = '修改工程管理系统-项目进度信息'
        this.getProgressDetail(row)
      } else {
        this.title = '添加工程管理系统-项目进度信息';
        this.form.projectId = this.$route.query.mainId;
      }
    },
    /** 获取进度信息详情 */
    getProgressDetail(row) {
      const id = row.id
      getProgress(id).then(response => {
        this.form = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProgress(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$parent.getList();
            });
          } else {
            addProgress(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$parent.getList();
            });
          }
        }
      });
    },
    /**取消按钮 */
    cancel() {
      this.open = false;
      this.reset()
    }
  }
}
</script>

<style lang="scss">
</style>